﻿using DevExpress.XtraEditors;
using SX_MesProject.Dtos.LJQDtos;
using SX_MesProject.Dtos;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using static SX_MesProject.HttpClientHelper;
using Newtonsoft.Json;

namespace SX_MesProject.ProcessManage
{
    public partial class ProcedureBindAdd : DevExpress.XtraEditors.XtraForm
    {
        public delegate Task LoadData();
        public event LoadData OnLoadData;
        private int ProcedureId;
        public ProcedureBindAdd(int id)
        {
            InitializeComponent();
            ProcedureId = id;
        }

        private void ProcedureBindAdd_Load(object sender, EventArgs e)
        {
            Databinds();
            //单击事件
            gridView1.RowClick += GridView_RowClick;
        }

        private async void Databinds()
        {
            try
            {
                var list = await HttpClientHelper.RequestUrl<string, ResponseDto<List<ProcedureBindEquipmentDto>>>("https://localhost:44556/", "GetEquipmentList", MethodType.GET);
                this.gridControl1.DataSource = list.Data;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }
        public int Value { get; set; }
        private void GridView_RowClick(object sender, DevExpress.XtraGrid.Views.Grid.RowClickEventArgs e)
        {
            // 这里是点击行时要执行的逻辑
            DevExpress.XtraGrid.Views.Grid.GridView view = sender as DevExpress.XtraGrid.Views.Grid.GridView;
            if (e.RowHandle >= 0)
            {
                // 获取点击行的数据
                object rowData = view.GetRow(e.RowHandle);
                // 进行相应的处理
                string rowData1 = JsonConvert.SerializeObject(rowData);
                var list = Newtonsoft.Json.JsonConvert.DeserializeObject<ProcedureBindEquipmentDto>(rowData1);
                this.Value = list.EquipmentId;
            }
        }

        private void simpleButton1_Click(object sender, EventArgs e)
        {
            AddBinds();
        }
        public async void AddBinds()
        {
            try
            {
                InsertProcedureBindEquimentDtos insertBind = new InsertProcedureBindEquimentDtos();
                insertBind.Procedure_Id = this.ProcedureId;
                insertBind.EquipmentId = this.Value;
                var list = await HttpClientHelper.RequestUrl<InsertProcedureBindEquimentDtos, ResponseDto<int>>("https://localhost:44556/", "InsertProcedureBindEquipment", MethodType.POST, insertBind);
                var res = list;
                if (list.Code == 200)
                {
                    MessageBox.Show("添加成功！");
                    //执行事件，调用父窗体的LoadData方法
                    OnLoadData?.Invoke();
                    //if (OnLoadData != null) OnLoadData.Invoke();
                    this.Close();
                }
            }
            catch (Exception ex)
            {

                MessageBox.Show(ex.Message);
            }
        }
    }
}